<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/2
 * Time: 11:24
 */

namespace app\index\logic;


use app\common\utils\Constant;
use think\Db;
use think\Exception;
use think\Log;

class PayLogic
{
    /**
     * @param $order_number string 订单编号
     * @param $money float 总金额
     * @param  $type 0微信支付 1支付宝支付 2银行卡支付
     * @return int 0余额不足 1支付成功 2支付失败
     */
    public function weChatPaySuccess($order_number,$money,$type)
    {
        $where['number']=$order_number;
        $order = model('Order')->where($where)->find();
        if($order){
//            if($recharge_order['state'] == 0 && is_numeric($money) && $recharge_order['money'] <= $money) {
            if($order['is_pay'] == 0 && is_numeric($money)) {
                return $this->paySuccess($order);//支付成功
            }
        }
        return 2;
    }

    public function paySuccess($order){
        Db::startTrans();
        try {
            $message_logic = new MessageLogic();
            if ($order['type'] == Constant::ORDER_TYPE_ANSWER) {
                $status = model('RedEnvelope')->where(array('id'=>$order['red_envelope_id']))->value('status');
                if($status != Constant::RED_ENVELOPE_STATUS_NO_CHALLENGE){
                    //如果不是未挑战
                    $logic = new RedEnvelopeLogic();
                    $rtn_data = $logic->refundByWeChat($order['number'], $order['money']);//退款
                    if ($rtn_data['status'] == 1) {
                        //更新红包状态
                        $update_order['is_pay'] = 1;
                        $update_answer_log['status'] = Constant::ANSWER_STATUS_END;
                        $update_answer_log['is_pay'] = 1;
                        model('AnswerLog')->where(array('id'=>$order['answer_log_id']))->update($update_answer_log);
                        $update_order['pay_type'] = Constant::ORDER_PAY_TYPE_WE_CHAT;//微信支付
                        model('Order')->where(array('id' => $order['id']))->update($update_order);
                        $message = '该挑战现在无法答题，已退回答题金';
                        $message_logic->sendMessageText($order['user_id'],$message);
                        Db::commit();
                    }
                    return 1;
                }
                $logic = new AnswerLogic();
                $logic->rewardAnswer($order['user_id']);
                model('AnswerLog')->where(array('id'=>$order['answer_log_id']))->setField('is_pay',1);
                model('RedEnvelope')->where(array('id'=>$order['red_envelope_id']))
                    ->setField('status',Constant::RED_ENVELOPE_STATUS_IN_THE_CHALLENGE);//修改为正在答题中
                $send_user_id = model('RedEnvelope')->where(array('id'=>$order['red_envelope_id']))->value('user_id');
                $message_logic->sendMessage($send_user_id,Constant::CONFIG_NAME_ANSWER_NOTICE);
                //答题金进入红包发送者账户
                $logic->returnMoneyToSender($order['red_envelope_id'],$order['user_id']);
            }else {
                model('RedEnvelope')->where(array('id' => $order['red_envelope_id']))->setField('is_pay', 1);
                $message_type = Constant::CONFIG_NAME_SEND_RED_ENVELOPE;
                $message_logic->sendRedEnvelopeMessage($order['user_id'],$message_type,$order['red_envelope_id']);
            }
            //更新红包状态
            $update_order['is_pay'] = 1;
            $update_order['pay_type'] = Constant::ORDER_PAY_TYPE_WE_CHAT;//微信支付
            model('Order')->where(array('id'=>$order['id']))->update($update_order);
            Db::commit();
            if($order['type'] == Constant::ORDER_TYPE_ANSWER){//长时间未答题自动结算
                $user_id = $order['user_id'];
                $answer_log_id = $order['answer_log_id'];
                pclose(popen("php /www/RedEnvelope/public/index.php /index/challenge/auto_end_answer/user_id/{$user_id}/answer_log_id/{$answer_log_id} &", 'r'));
            }
            return 1;
        }catch (Exception $e){
            Db::rollback();
            Log::write('paySuccess:'.$e->getMessage());
            return false;
        }
    }

}